package hamaster.jpbc.userconfig;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 用户配置信息
 * @author <a href="mailto:wangyeee@gmail.com">Wang Ye</a>
 */
public class UserConfig {

	private Properties properties;

	private Properties change;

	public UserConfig() {
		properties = new Properties();
		change = new Properties();
	}

	public String getProperty(String key) {
		return properties.getProperty(key);
	}

	public synchronized void setProperty(String key, String value) {
		String old = (String) properties.setProperty(key, value);
		if (old != null)
			change.setProperty(key, old);
	}

	public void store(File file) throws FileNotFoundException, IOException {
		FileOutputStream out = new FileOutputStream(file);
		properties.store(out, null);
		out.close();
	}

	public synchronized void discard() {
		for (Object key : change.keySet()) {
			properties.setProperty((String) key, (String) change.get(key));
		}
	}
}
